// pages/reservationList/reservationList.js
let app = getApp()
import { requestApi } from '../../api'
import { formatImage } from '../../utils/formatImage'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    winWidth: 0,
    winHeight: 0,
    scrollTop: 0,
    reservationList: [],
    activeStatus: 1,
    loading: false,
    start: 0,
    count: 20,
    noMore: false,
    requestFlag: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let channel = options.channel || 'wx_xcx'

    this.data.channel = channel
    this.getReservationList(this.data.start,channel)

    let systemInfo = {}

    try {
      systemInfo = wx.getSystemInfoSync()
    } catch (e) {
      // Do something when catch error
    }

    this.setData({
      winWidth: systemInfo.windowWidth,
      winHeight: systemInfo.windowHeight,
    })
  },

  getReservationList: function (start, channel) {
    if(this.data.noMore) return false
    if (this.data.requestFlag) return false

    this.setData({
      requestFlag: true,
    })
    app.request({
      url: requestApi.reservationListUrl,
      methods: 'GET',
      data: {
        start: start,
        count: this.data.count,
        channel: channel
      }
    }, (data) => {
      let reservationList, currentData, noMore

      if (data.original.data.item_list && data.original.data.item_list.length) {
        reservationList = data.original.data.item_list
        reservationList.forEach((item) => {
          if (item.data.activity_image && item.data.activity_image.path) {
            item.data.activity_image.new_path = formatImage.cdnImage(item.data.activity_image.path, formatImage.THUMBNAIL_L)
          }
        })
      }

      currentData = this.data.reservationList
      reservationList = currentData.concat(data.original.data.item_list)

      if (data.original.data.item_list.length < this.data.count) {
        noMore = true
      } else {
        noMore = false
      }

      this.setData({
        reservationList: reservationList,
        loading: true,
        start: start + this.data.count,
        noMore: noMore
      })
    }, (error) => {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '错误',
      })
    }, () => {
      this.setData({
        requestFlag: false,
      })
    })
  },

  scrolltolower: function () {
    this.getReservationList(this.data.start, this.data.channel)
  },

  reservationTap: function (e) {
    let $status = e.currentTarget.dataset.status
    let activityId = e.currentTarget.dataset.id
    let tradeNo = e.currentTarget.dataset.tradeno

    if ($status == 2) {
      wx.navigateTo({
        url: '../reservation/reservation?activity_id=' + activityId,
      })
    } else {
      wx.navigateTo({
        url: '../reservationResult/reservationResult?reservationStatus=' + $status + '&activity_id=' + activityId + '&trade_no=' + tradeNo,
      })
    }
  },

  paymentTap (e) {
    let tradeNo = e.currentTarget.dataset.id

    if (!tradeNo) return false
    let url = '../trade/tradeDetail/tradeDetail?trade_no=' + tradeNo + '&channel=' + this.data.channel + '&activeStatus=' + this.data.activeStatus

    wx.navigateTo({
      url: url,
    })

    return false
  },

  orderTap (e) {
    let tradeNo = e.currentTarget.dataset.id

    if (!tradeNo) return false
    let url = '../trade/tradeDetail/tradeDetail?trade_no=' + tradeNo + '&channel=' + this.data.channel + '&activeStatus=' + this.data.activeStatus

    wx.navigateTo({
      url: url,
    })

    return false
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {
  
  // }
})